// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer.provider;

/**
 * Contains information about the locales of a vocable (e.g. English and German), and the vocable 
 * values.
 * 
 * @author Teresa Holfeld, teresa.holfeld@googlemail.com
 *
 */
public class VocablePair {

	private String mLocale1;
	private String mLocale2;
	private String mVocable1;
	private String mVocable2;
	
	/**
	 * @param locale1 e.g. "English"
	 * @param locale2 e.g. "German"
	 * @param vocable1 e.g. "Apple"
	 * @param vocable2 e.g. "Apfel"
	 */
	public VocablePair(String locale1, String locale2, String vocable1, String vocable2) {
		mLocale1 = locale1;
		mLocale2 = locale2;
		mVocable1 = vocable1;
		mVocable2 = vocable2;
	}

	/**
	 * @return the mLocale1
	 */
	public String getLocale1() {
		return mLocale1;
	}

	/**
	 * @return the mLocale2
	 */
	public String getLocale2() {
		return mLocale2;
	}

	/**
	 * @return the mVocable1
	 */
	public String getVocable1() {
		return mVocable1;
	}

	/**
	 * @return the mVocable2
	 */
	public String getVocable2() {
		return mVocable2;
	}
	
	/**
	 * Gets the vocable belonging to the given locale.
	 * 
	 * @param locale e.g. "En" or "De"
	 * @return the vocable
	 */
	public String getVocable(String locale) {
		if (locale.equals(mLocale1)) {
			return mVocable1;
		} else if (locale.equals(mLocale2)) {
			return mVocable2;
		} else {
			throw new IllegalArgumentException("Wrong locale " + locale);
		}
	}
	
	/**
	 * A vocable pair is equal to another if it contains the same values of the same locales. It 
	 * is equal even if they are interchanged.
	 * 
	 * <p>
	 * For example:<br>
	 * "En", "De" -&gt; "Apple", "Apfel" <b>equals</b> "En", "De" -&gt; "Apple", "Apfel"<br>
	 * "En", "De" -&gt; "Apple", "Apfel" <b>equals</b> "De", "En" -&gt; "Apfel", "Apple"<br>
	 * "En", "De" -&gt; "Apple", "Apfel" <b>does not equal</b> "De", "En" -&gt; "Apple", "Apfel"
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object aThat) {
		if (!(aThat instanceof VocablePair)) return false;
		VocablePair that = (VocablePair) aThat;
		
		// right way
		if (that.mLocale1.equals(mLocale1)) {
			return (that.mLocale2.equals(mLocale2) && that.mVocable1.equals(mVocable1)
					&& that.mVocable2.equals(mVocable2));
		} 
		// the other way around
		else if (that.mLocale1.equals(mLocale2)) {
			return (that.mLocale2.equals(mLocale1) && that.mVocable1.equals(mVocable2)
					&& that.mVocable2.equals(mVocable1));
		} else {
			return false;
		}
	}
}
